﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Windows.Data;


namespace Microsoft.Forums.ForumsClient
{
    /// <summary>
    ///  Formatter class using IValueConverter to format WPF data bound values.
    /// </summary>
    public class FormatToString : IValueConverter
    {
        /// <summary>
        ///  Convert Implementation.
        /// </summary>
        /// <param name="value">Value that needs to be formatted</param>
        /// <param name="targetType">The type of the binding target property</param>
        /// <param name="param">Format String</param>
        /// <param name="culture">The culture to use in the formatter</param>
        /// <returns>formatted string</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (targetType != typeof(string))
                throw new InvalidCastException("Can only format to string with FormatToString");

            string formatString = parameter as string;

            if (formatString != null)
            {
                return string.Format(culture, formatString, value);
            }
            else
                return value.ToString();
        }

        /// <summary>
        ///  Note that this is not supported currently for this converter.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target</param>
        /// <param name="targetType">target Type to convert to</param>
        /// <param name="param">Format string</param>
        /// <param name="culture">Culture to be used in convertback</param>
        /// <returns>throws NotSupportedException</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("ConvertBack is not supported for FormatToString");
        }

    }
}